package com.suixingfu.utils;

import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

import org.apache.commons.io.IOUtils;

import com.suixingfu.vo.RequestMessage;

public class Encrypt {
	// 加密
	public static void main(String[] args) throws Exception {

//		String json = "{'clientId':'836100058122636','version':'1.0','reqId':'2015122516091210001','tranCd':'DF1001','reqData':{}}";// 客户请求的json数据
		String json = "{'clientId':'600000000000182','version':'1.0','reqId':'2015122516091210001','tranCd':'DF1001','reqData':{}}";// 客户请求的json数据
		
		RequestMessage erm = JsonUtils.fromJson(json, RequestMessage.class);

		String reqData = erm.getReqData();

		// DES密钥加密
		String DESkey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCiZqyKJeaRJh+AU1EWJhkMXFPsQdVHKeP+tbqdYYw/Pi+vEJ4QhvVZ/YFcv7K8eBpMn71UPo8CP2Ere81qjdJrGDJXnQcRIcJe2TLJv070Vt0fNw4rBpkNJDdhSNwPtsT9UaAvrhi/tX8gvT+2gqZMEm7hl5L6PSA3W/sfFrAYZQIDAQAB";

		byte[] bs = DESUtils.encrypt(reqData.getBytes("UTF-8"), DESkey);
		// 编码
		String reqDataEncrypt = Base64Utils.encode(bs);
		erm.setReqData(reqDataEncrypt);

		// 合作商私钥签名
		String RSAKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJ2RQPh70xBdszWvblWQM53BAodNNUdVE9gaLZpe2LUwv15BMxnur47gheaTQ3t3XIaGccre+clqaSkIN6niTcTIk5LgPHUD6Tp0CUqxHavn2lZy/AJ6vZ7yFTxZXmBckii7+bunT8EyNpZVhN3d3pIl3spNQyPxyV4vtfW+PqKxAgMBAAECgYBVh2ETAHPDMHl0rLNhcpLyELDErN48yCurZBpY7x8Gy101VoLckrls/ubwEAPTYykwRI5EJuDDuiU3BqTgbFz1kdayLV7YZSICgRZJEBtrtmoLku9zH2xFP2PY0dB5p8EdkPn4TsJfTyVER0PWy8IMWIhLenPyL2ubADnTTvyIAQJBAP+rFdsUsRi1ftZzQd1d+4vPotw1qoSi/q6Kp47hA+R4mhtmI2AQKLERTwvyxZ8u3JW1EpFsld4X3oGlzrhwqXECQQCdxZYbGLoPXhtrNN6RXe+lCt/TFiZYEF+sLntcn0YZ+0QHs897moNLGPZtIj/4nhAwUYcCd2a/JzKm/YvWJu1BAkAB5lyQNzQlsduSbmLT6UngPRfBuSheOfPYijE6j5N+1CKZB4ZFxXj379/9f71WNHvxJNpReFQYxWai4f+KQVQRAkBJXVbmtMHYYY9oc8ZHXKISYhMgqj0cWEjU3Sx6Loo/jKre1gtWU8xXwHze1L1gk8e9YDvitrktjEGui1paM/HBAkEArgDBrsWfNKqK0kwFG6r9HN/b4cj5VR1E7w89FJayKm9fzUbXeC1aESk4IfpteLibtX0onxVaWnseg5QKumIREw==";

		erm.setSign(RSAUtils.sign(reqDataEncrypt, RSAKey));

		System.out.println(JsonUtils.toJson(erm));
		String jstr = JsonUtils.toJson(erm);
		String url = "http://106.37.197.53:38080/paygateway/queryAcc.do";

		URL urlt = new URL(url);
		URLConnection urlConnection = urlt.openConnection();
		// 设置doOutput属性为true表示将使用此urlConnection写入数据
		urlConnection.setDoOutput(true);
		// 定义待写入数据的内容类型，我们设置为application/x-www-form-urlencoded类型
		urlConnection.setRequestProperty("content-type", "application/json;charset=utf-8");
		urlConnection.setRequestProperty("accept", "application/json;chaeset=utf-8");
		// 得到请求的输出流对象
		OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
		// 把数据写入请求的Body
		// JSONObject reqheadj = JSONObject.fromObject(reqhead);
		out.write(jstr);
		out.flush();
		out.close();

		// 从服务器读取响应
		InputStream inputStream = urlConnection.getInputStream();
		String encoding = urlConnection.getContentEncoding();
		String body = IOUtils.toString(inputStream, encoding);
		System.out.println(body);

	}

}
